package com.getpebble.android.framework;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.model.FrameworkState;
import com.sun.mail.smtp.SMTPMessage;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class FrameworkEventReceiver {
    public static final String TAG = FrameworkEventReceiver.class.getSimpleName();
    private static FrameworkState sLatestFrameworkState;
    private static List<IFrameworkEventListener> sListeners = new CopyOnWriteArrayList();
    private Messenger mMessenger;
    private Handler mUiHandler;

    public interface IFrameworkEventListener {
        void onFrameworkStateChanged(FrameworkState frameworkState);
    }

    private class IncomingHandler extends Handler {
        public IncomingHandler(Looper looper) {
            super(looper);
        }

        public void handleMessage(Message msg) {
            try {
                doHandleMessage(msg);
            } catch (Exception e) {
                Trace.error(FrameworkEventReceiver.TAG, "Exception while handling message.", e);
            }
        }

        private void doHandleMessage(Message msg) {
            switch (msg.what) {
                case SMTPMessage.RETURN_FULL /*1*/:
                    Bundle data = msg.getData();
                    data.setClassLoader(FrameworkState.class.getClassLoader());
                    final FrameworkState state = (FrameworkState) data.getParcelable("state_extra");
                    FrameworkEventReceiver.this.mUiHandler.post(new Runnable() {
                        public void run() {
                            Trace.debug(FrameworkEventReceiver.TAG, "Event: " + state.getLastEvent() + " Number of listeners: " + FrameworkEventReceiver.sListeners.size());
                            synchronized (FrameworkEventReceiver.class) {
                                FrameworkEventReceiver.sLatestFrameworkState = state;
                                for (IFrameworkEventListener listener : FrameworkEventReceiver.sListeners) {
                                    listener.onFrameworkStateChanged(state);
                                }
                            }
                        }
                    });
                    return;
                default:
                    super.handleMessage(msg);
                    return;
            }
        }
    }

    FrameworkEventReceiver(Context context) {
        this.mUiHandler = new Handler(context.getMainLooper());
        HandlerThread thread = new HandlerThread("EventReceiver");
        thread.start();
        this.mMessenger = new Messenger(new IncomingHandler(thread.getLooper()));
    }

    Messenger getMessenger() {
        return this.mMessenger;
    }

    public static synchronized void registerFrameworkStateEventListener(IFrameworkEventListener eventListener) {
        synchronized (FrameworkEventReceiver.class) {
            if (eventListener == null) {
                throw new IllegalArgumentException("ConnectionEventListener cannot be null!");
            }
            sListeners.add(eventListener);
            Trace.debug(TAG, "Added listener, count is now " + sListeners.size());
        }
    }

    public static synchronized void unregisterFrameworkStateEventListener(IFrameworkEventListener eventListener) {
        synchronized (FrameworkEventReceiver.class) {
            sListeners.remove(eventListener);
            Trace.debug(TAG, "Removed listener, count is now " + sListeners.size());
        }
    }

    public static synchronized FrameworkState getLatestFrameworkState() {
        FrameworkState frameworkState;
        synchronized (FrameworkEventReceiver.class) {
            frameworkState = sLatestFrameworkState;
        }
        return frameworkState;
    }
}
